package com.erlang.sql.jdbc;

import com.mysql.jdbc.Driver;
import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * Jdbc 注册驱动,获取连接
 *
 * @author ZGQ
 */
public class DriverManagerDemo {
  /**
   * 注册驱动方式一
   *
   * @throws SQLException
   */
  @Test
  public void test01() throws SQLException {
    // 这样会导致驱动注册两次数
    DriverManager.registerDriver(new Driver());
  }

  /**
   * 注册驱动方式二
   *
   * @throws ClassNotFoundException
   */
  @Test
  public void test02() throws ClassNotFoundException {
    // 这样驱动会注册一次
    Class.forName("com.mysql.jdbc.Driver");
  }

  /**
   * 获取连接
   *
   * @throws ClassNotFoundException
   * @throws SQLException
   */
  @Test
  public void test03() throws ClassNotFoundException, SQLException {
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/spring", "root", "1234");
    System.out.println(conn);
  }
}
